############# ########### ####### ##### ### ##
#
# Copyright (C) 2010 The Hursk Group, LLC
#
# This program is free software: you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (See file named COPYING).  If not, see 
# <http://www.gnu.org/licenses/>.
# 
############# ########### ####### ##### ### ##

#!/bin/tcsh

# Output pkginfo file 
setenv pkgoutputfile "/tmp/pkginfo.out"

# Output file 
setenv outputfile "/path/to/patchdiag.csv"
touch "$outputfile"; rm "$outputfile"

# Location of the patchdiag.xref file
# http://sunsolve.sun.com/pdownload.do?target=patchdiag.xref
setenv patchdiag "/path/to/patchdiag.xref"
[ -e "$patchdiag" ] || echo "File "$patchdiag" does not exist" && exit 1 


# Location of the sed file that substitutes ip to host
setenv iphostsed "/path/to/host_ip.sed"
[ -e "$iphostsed" ] || echo "File "$iphostsed" does not exist" && exit 1 

# Location of the pkginfo awk script
setenv pkginfoawk "/path/to/pkginfo_parser.awk"
[ -e "$pkginfoawk" ] || echo "File "$pkginfoawk" does not exist" && exit 1 

# Location of the awk script that does the magic
setenv patchdiagawk "/path/to/patchdiag.awk"
[ -e "$patchdiagawk" ] || echo "File "$patchdiagawk" does not exist" && exit 1 

# Location of the sed file that substitutes ip to host
#setenv iphostsed "/path/to/host_ip.sed"
#[ -e "$iphostsed" ] || echo "File "$iphostsed" does not exist" && exit 1 

# Directory of the extracted .tbz2 files
setenv dirtbz2 "/path/to"
cd $dirtbz2 || echo "Directory "$dirtbz2" does not exist" && exit 1

# First run the pkginfo script to get out input in a good format
foreach dirs ( `ls -d * ` )  
awk -f "$pkginfoawk" ${dirs}/pkginfo.long.out > "$pkgoutputfile"
awk -v ip=$dirs -f "$patchdiagawk" pass=1 ${dirs}/showrevp.out pass=2 "$pkgoutputfile" pass=3 ${dirs}/uname.out pass=4 "$patchdiag"  >> "$outputfile"
end

sed -i "" -f "$iphostsed" "$outputfile"
